<template>
  <div class="com-container">
    <div class="com-chart" ref="sex_ref"></div>
  </div>
</template>

<script>
export default {
  props: ['admissionyear','academy','status'],
  watch: {
    admissionyear: {
      handler(newValue,oldValue) {
        this.getData()
      }
    },
    academy: {
      handler(newValue,oldValue) {
        this.getData()
      }
    },
    status: {
      handler(newValue,oldValue) {
        this.getData()
      }
    }
  },
  data() {
    return {
      chartInstance: null,
      allData: [],
    };
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener('resize', this.screenAdapter());
    //在页面加载完成后主动进行页面的自适应
    this.screenAdapter();
  },
  //在组件销毁时，进行监听事件的取消
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter())
  },
  methods: {
    // 初始化echartInstance对象
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.sex_ref,'chalk');
      const initOption = {
        title: {
          text:'性别比例'
        },
        tooltip: {
          trigger: "item",
        },
        series: [
          {
            name: "性别比例",
            type: "pie",
            //设置圆环比例
            radius: ["35%", "70%"],
            avoidLabelOverlap: false,
            itemStyle: {
              borderColor: "#fff",
              borderWidth: 1,
            },
            label: {
              show: true,
              position: "outside",
              fontWeight: "bold",
              formatter:'{b}:{d}%'
            },
            labelLine: {
              show: false,
            },
          },
        ],
      };
      this.chartInstance.setOption(initOption);
    },
    // 数据
    getData() {

      this.request.get("/stuScreen/sex",{
        params: {
          admissionyear: this.admissionyear,
          academy: this.academy
        }
      }).then(res=>{
        if(res.code==20041) {
          this.allData=res.data
        }
      }).finally(()=>{
        this.updateChart()
      })
    },

    // 更新图表
    updateChart() {
      const dataOption = {
        series: [
          {
            data: this.allData,
          },
        ],
      };
      this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      const sizeModel = (this.$refs.sex_ref.offsetWidth / 100) * 7.2;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize:sizeModel/2
          }
        },
        series: [
          {
            itemStyle: {
              borderRadius: sizeModel*0.1,
            },
            label: {
              fontSize: sizeModel/2
            },

          },
        ],
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
  },
};
</script>

<style lang="less" scoped>
</style>